package com.distributer.delayqueue.zookeeper;

import org.apache.curator.framework.recipes.leader.LeaderLatchListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.distributer.delayqueue.delayqueue.Queue;

public class ZookeeperListener implements LeaderLatchListener{

	private static final Logger LOGGER = LoggerFactory.getLogger(ZookeeperListener.class);
	
	private Queue queue;
	
	private String port;
	    
	public ZookeeperListener(){
	}
	
	public void setQueue(Queue queue,String port) {
		this.queue = queue;
		this.port = port;
	}
	
	@Override
	public void isLeader() {
		LOGGER.info(String.format("客户端端口号: %s 获取到了执行权限", port)); 
		queue.start();
	}

	@Override
	public void notLeader() {
		LOGGER.info(String.format("客户端端口号: %s 放弃了执行权限", port)); 
		queue.stop();
	}

}
